package cn.springspace.kotlinapp

import cn.springspace.app.service.ArtemisService
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.event.ApplicationReadyEvent
import org.springframework.boot.runApplication
import org.springframework.context.ApplicationEvent
import org.springframework.context.ApplicationListener
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.event.ContextRefreshedEvent
import org.springframework.context.event.EventListener
import org.springframework.context.support.beans
import java.util.*
import kotlin.concurrent.thread

@SpringBootApplication
@ComponentScan(basePackages = ["cn.springspace"])
open class KotlinAppApplication {
    @EventListener
    fun requestHandledEventListener(event: ApplicationEvent) {
        println("系统时间：${Calendar.getInstance()} 事件名称：${event.javaClass.simpleName}")
    }
}

fun main(args: Array<String>) {
   var context = runApplication<KotlinAppApplication>(*args) {
        addInitializers(
                beans {
                    bean("contextRefreshedEventListener") {
                        ApplicationListener<ContextRefreshedEvent> { event ->
                            println("容器刷新事件：$event")
                        }
                    }
                    bean("applicationReadyEventListener") {
                        ApplicationListener<ApplicationReadyEvent> { event ->
                            println("容器启动完成事件：$event")
                        }
                    }
                }
        )
    }
    var artemisService = context.getBean(ArtemisService::class.java);
    while (true) {
        thread(start = true) {
            // 在这里编写你的代码
            println("Thread is running")
            artemisService.refreshDoorState();
            artemisService.refreshDoorEvent();
        }
        Thread.sleep(1000 * 60);
    }
}
